package cn.walls1717.mall.modules.pms.controller;


import cn.walls1717.mall.common.api.CommonPage;
import cn.walls1717.mall.common.api.CommonResult;
import cn.walls1717.mall.common.util.ComResultUtil;
import cn.walls1717.mall.modules.pms.dto.PageListDTO;
import cn.walls1717.mall.modules.pms.dto.ProductCateChildrenDTO;
import cn.walls1717.mall.modules.pms.dto.ProductCateDTO;
import cn.walls1717.mall.modules.pms.model.PmsProductCategory;
import cn.walls1717.mall.modules.pms.service.PmsProductCategoryService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 产品分类 前端控制器
 * </p>
 *
 * @author walls1717
 * @since 2022-04-09
 */
@RestController
@RequestMapping("/productCategories")
public class PmsProductCategoryController {

    private PmsProductCategory productCategory;

    private final PmsProductCategoryService categoryService;
    @Autowired
    public PmsProductCategoryController(PmsProductCategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @ApiOperation("商品分类分页展示")
    @GetMapping(path = "list/{parentId}")
    public CommonResult<CommonPage<PmsProductCategory>> pageList(@Valid PageListDTO pageListDTO) {
        Page<PmsProductCategory> list = categoryService.pageList(pageListDTO);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("商品分类添加")
    @PostMapping()
    public CommonResult<?> createCateGory(@RequestBody ProductCateDTO productCateDTO) {
        boolean result = categoryService.createCategory(productCateDTO);
        return ComResultUtil.booleanResult(result);
    }
    @ApiOperation("商品分类编辑")
    @PutMapping("{id}")
    public CommonResult<Object> updateCategory(@PathVariable("id") Long id, @RequestBody ProductCateDTO productCateDTO) {
        boolean flag = categoryService.updateCategory(id, productCateDTO);
        if (flag) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("商品分类删除")
    @DeleteMapping("{id}")
    public CommonResult<?> deleteCategory(@PathVariable("id") Integer id) {
        boolean result = categoryService.deleteCategory(id);
        return ComResultUtil.booleanResult(result);
    }

    @ApiOperation("商品分类导航栏状态")
    @PutMapping("navStatus")
    public CommonResult<?> updateNavStatus(@RequestParam("ids") List<Long> ids, @RequestParam("navStatus") Integer nav) {
        productCategory = new PmsProductCategory();
        productCategory.setNavStatus(nav);
        boolean result = categoryService.updateNavAndShowStatus(ids, productCategory);
        return ComResultUtil.booleanResult(result);
    }

    @ApiOperation("商品分类是否显示状态")
    @PutMapping("showStatus")
    public CommonResult<?> updateShowStatus(@RequestParam("ids") List<Long> ids, @RequestParam(value = "showStatus", defaultValue = "0") Integer showStatus) {
        productCategory = new PmsProductCategory();
        productCategory.setShowStatus(showStatus);
        boolean result = categoryService.updateNavAndShowStatus(ids, productCategory);
        return ComResultUtil.booleanResult(result);
    }

    @ApiOperation("商品分类编辑页面数据初始化")
    @GetMapping("{id}")
    public CommonResult<PmsProductCategory> getOneCategory(@PathVariable Long id) {
        PmsProductCategory byId = categoryService.getById(id);
        return CommonResult.success(byId);
    }

    @ApiOperation("商品列表页面，商品分类级联下拉列表")
    @GetMapping("withChildren")
    public CommonResult<List<ProductCateChildrenDTO>> withChildren() {
        List<ProductCateChildrenDTO> list = categoryService.withChildren();
        return CommonResult.success(list);
    }

}

